import Vue from 'vue'
import VueRouter from 'vue-router'

// 把这段代码直接粘贴到router/index.js中的Vue.use(VueRouter)之前
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => { })
};
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login/Login.vue')
  },
  {
    path: '*',
    redirect: '/login'
  },
  {
    path: '/',
    component: () => import('../views/Main/Main.vue'),
    children: [
      {
        path: '/index',
        name: 'index',
        component: () => import('../views/Indexs/Indexs.vue'),
      },
      {
        path: '/', redirect: '/index'
      }, {
        path: '/menu',
        name: 'menu',
        meta: {
          title1: "系统设置",
          title2: "菜单管理",
        },
        component: () => import('../views/Menu/Menu.vue'),
      }, {
        path: '/role',
        name: 'role',
        meta: {
          title1: "系统设置",
          title2: "角色管理",
        },
        component: () => import('../views/Role/Role.vue'),
      }, {
        path: '/user',
        name: 'user',
        meta: {
          title1: "系统设置",
          title2: "管理员管理",
        },
        component: () => import('../views/User/User.vue'),
      }, {
        path: '/category',
        name: 'category',
        meta: {
          title1: "商城管理",
          title2: "商品分类",
        },
        component: () => import('../views/Category/Category.vue'),
      }, {
        path: '/specs',
        name: 'specs',
        meta: {
          title1: "商城管理",
          title2: "商品规格",
        },
        component: () => import('../views/Species/Specs.vue'),
      }, {
        path: '/goods',
        name: 'goods',
        meta: {
          title1: "商城管理",
          title2: "商品管理",
        },
        component: () => import('../views/Goods/Goods.vue'),
      }, {
        path: '/member',
        name: 'member',
        meta: {
          title1: "商城管理",
          title2: "会员管理",
        },
        component: () => import('../views/Member/Member.vue'),
      }, {
        path: '/banner',
        name: 'banner',
        meta: {
          title1: "商城管理",
          title2: "轮播图管理",
        },
        component: () => import('../views/Banner/Banner.vue'),
      }, {
        path: '/seckill',
        name: 'seckill',
        meta: {
          title1: "商城管理",
          title2: "秒杀活动",
        },
        component: () => import('../views/Seckill/Seckill.vue'),
      },
    ]
  },

]

const router = new VueRouter({
  routes
})

// 全局前置路由守卫
router.beforeEach((to, from, next) => {
  let token = sessionStorage['userInfo_pc'] ? JSON.parse(sessionStorage['userInfo_pc']).token : ""
  if (to.path == '/login') {
    return next()
  }
  // 判断有没有登录
  if (token) {
    return next()
  }
  // 没有登录
  Vue.prototype.$message.warning('请先登录')
  next('/login')
})

export default router
